#ifndef GNAPP_WIN32_H
#include "GnApplicationPCH.h"
#include "GnApp.h"
#include "GnLog.h"

#if defined(WIN32)
int GnApp::StartWinApp(HINSTANCE instance, LPTSTR cmdLine, int nCmdShow)
{
	GnStartup();

	GnLogger::UseUserSystemLog();
	GnLogger::GetInstance()->SetUseLogger(GnText("AppLog"));


	GnApp* gnApp = GnApp::Create();
	if (!gnApp)
	{
		GnError( GnText("Failed Create GnApp") );
		return 0;
	}

	msWindowInstance = instance;


	// Now, we are ready to initialize the window and the main loop.

	WNDCLASS wC;
	wC.style         = CS_HREDRAW | CS_VREDRAW;
	wC.lpfnWndProc   = GnApp::WndProc;
	wC.cbClsExtra    = 0;
	wC.cbWndExtra    = 0;
	wC.hInstance     = instance;
	wC.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wC.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wC.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wC.lpszClassName = GnAppWindow::GetWindowClassName();
	if (gnApp->GetMenuID())
		wC.lpszMenuName = MAKEINTRESOURCE(gnApp->GetMenuID());
	else
		wC.lpszMenuName = 0;

	RegisterClass(&wC);

	// create the application window
	if(!gnApp->CreateGnWindow(nCmdShow, instance))
	{
		GnError(GnText("Failed Create Window"));
		return 0;
	}
	if ( gnApp->CreateRenderer() )
	{
	}

	// allow the application to initialize before starting the message pump
	if (gnApp->Init())
	{
		gnApp->Win32Loop();
	}

	GnDelete gnApp;
	GnShutdown();
	return 0;
}

LRESULT CALLBACK GnApp::WndProc(GnWindowHandle hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if(msSingletone == NULL)
		return DefWindowProc(hWnd, message, wParam, lParam);

	int wmId, wmEvent;
	//PAINTSTRUCT ps;
	//HDC hdc;
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		break;
	case WM_PAINT:
		break;
	case WM_DESTROY:
		{
			//exit(1);
			PostQuitMessage(0);
		}
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

void GnApp::Win32Loop()
{
	while (true)
	{
		MSG msg;
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;

			if (!TranslateAccelerator(msSingletone->GetAppWindow()->GetMainWindowHandle(), NULL, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			MainLoop();
		}
	}
}

#endif // #if defined(WIN32)